//
//  TDTransactionDetailViewController.m
//  TFB
//
//  Created by yull on 15/8/14.
//  Copyright (c) 2015年 TD. All rights reserved.
//

#import "TDTransactionDetailViewController.h"
#import "TransactionDetailCell.h"

@interface TDTransactionDetailViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *leftArray;
@property (nonatomic, strong) NSMutableArray *rightArray;

@end

@implementation TDTransactionDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"订单详情";
    [self setupCustomBackItem];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.leftArray = @[@"订单号", @"下单时间", @"支付时间", @"实付金额", @"手续费", @"状态", @"收款方式", @"手机号", @"商品分类"];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.autoresizingMask = self.view.autoresizingMask;
    tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView registerNib:[UINib nibWithNibName:@"TransactionDetailCell" bundle:nil] forCellReuseIdentifier:@"TransactionDetailCell"];
    
    [self initData];
}

- (void)init_right_array_WithDic:(NSDictionary *)dic
{
    self.rightArray = [NSMutableArray array];
    
    NSString *payordno = self.payordno.length ? self.payordno : @"";
    [self.rightArray addObject:payordno];
    
    if (self.orderTime.length) {
        [self.rightArray addObject:[NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",
                                    [self.orderTime substringWithRange:NSMakeRange(0, 4)],
                                    [self.orderTime substringWithRange:NSMakeRange(4, 2)],
                                    [self.orderTime substringWithRange:NSMakeRange(6, 2)],
                                    [self.orderTime substringWithRange:NSMakeRange(8, 2)],
                                    [self.orderTime substringWithRange:NSMakeRange(10, 2)],
                                    [self.orderTime substringWithRange:NSMakeRange(12, 2)]]];
    } else {
        [self.rightArray addObject:@""];
    }

    NSString *modifyTime = [dic objectForKey:@"modifyTime"];
    if ([modifyTime isKindOfClass:[NSString class]] && modifyTime.length) {
        [self.rightArray addObject:[NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",
                                    [modifyTime substringWithRange:NSMakeRange(0, 4)],
                                    [modifyTime substringWithRange:NSMakeRange(4, 2)],
                                    [modifyTime substringWithRange:NSMakeRange(6, 2)],
                                    [modifyTime substringWithRange:NSMakeRange(8, 2)],
                                    [modifyTime substringWithRange:NSMakeRange(10, 2)],
                                    [modifyTime substringWithRange:NSMakeRange(12, 2)]]];
    } else {
        [self.rightArray addObject:@""];
    }
    
    NSString *txamt = [[dic objectForKey:@"txamt"] length] ? [dic objectForKey:@"txamt"] : @"";
    [self.rightArray addObject:txamt];
    
    NSString *fee = [dic objectForKey:@"fee"];
    fee = ([fee isKindOfClass:[NSString class]] && fee.length) ? fee : @"";
    [self.rightArray addObject:fee];
    
    [self.rightArray addObject:self.paystatus.length ? self.paystatus : @""];
    
    [self.rightArray addObject:@"店主代付"];
    
    NSString *paySignPic = [dic objectForKey:@"paySignPic"];
    if ([paySignPic isKindOfClass:[NSString class]] && paySignPic.length) {
        [self.rightArray addObject:paySignPic];
    } else {
        [self.rightArray addObject:@""];
    }
    
    [self.rightArray addObject:self.category.length ? self.category : @""];
    
}

- (void)initData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [TDHttpEngine requestFor_GETTRANDETAIL_WithCustId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] payordno:self.payordno complete:^(BOOL succeed, NSString *msg, id reqBody) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if (succeed) {
            
            [self init_right_array_WithDic:reqBody];
            [self.tableView reloadData];
        } else {
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}

#pragma mark - UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 24;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.leftArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TransactionDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TransactionDetailCell"];
    cell.leftL.text = self.leftArray[indexPath.row];
    cell.rightL.text = self.rightArray[indexPath.row];
    
    return cell;
}

@end
